<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="demo">
    姓: <input type="text" placeholder="姓氏" v-model="firstName"><br>
    名: <input type="text" placeholder="名字" v-model="lastName"><br>
    姓名1(单向): <input type="text" placeholder="姓名1" v-model="fullName1"><br>
    姓名2(单向): <input type="text" placeholder="姓名2"><br>
    姓名3(单向): <input type="text" placeholder="姓名3" v-model="fullName3">
  </div>

  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#demo',
      data: {
        firstName: 'A',
        lastName: 'B',
        fullName2: 'A-B'
      },
      //计算属性
      computed: {
        fullName1() {
          /*
          什么时候执行?
          1. 初始显示 执行一次
          2. 依赖数据发生变化
          */
          return this.firstName + '-' + this.lastName
        },

        fullName3: {
          get() {
            return this.firstName + '-' + this.lastName
          },
          set(value) {
            const names = value.split('-')
            this.firstName = names[0]
            this.lastName = names[1]
          }
        }
      },
      watch: {
        //当firstName的值发生改变时调用
        firstName(newValue) {
          //更新fullName2
          this.fullName2 = newValue + '-' + this.lastName
        }
      },
    })
    //另一种写法
    vm.$watch('lastName', function(newValue) {
      this.fullName2 = this.firstName + '-' + newValue
    })
  </script>
</body>
</html>